/**
 * 设置手机通知权限
 */
export const setPermissionsInform = () => {
  // #ifdef APP-PLUS
  if (plus.os.name === 'Android') {
    // 判断是Android
    const main: any = plus.android.runtimeMainActivity()
    const pkName = main.getPackageName()
    const uid = main.getApplicationInfo().plusGetAttribute('uid')
    let NotificationManagerCompat: any = plus.android.importClass(
      'android.support.v4.app.NotificationManagerCompat',
    )
    // android.support.v4升级为androidx
    if (NotificationManagerCompat == null) {
      NotificationManagerCompat = plus.android.importClass(
        'androidx.core.app.NotificationManagerCompat',
      )
    }
    const areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled()
    // 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
    if (!areNotificationsEnabled) {
      console.info(
        '-------------->未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置',
      )
      uni.showModal({
        title: '通知权限开启提醒',
        content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
        showCancel: true,
        confirmText: '去设置',
        cancelText: '稍后设置',
        success: function (res) {
          if (res.confirm) {
            const Intent: any = plus.android.importClass('android.content.Intent')
            const Build: any = plus.android.importClass('android.os.Build')
            // android 8.0引导
            if (Build.VERSION.SDK_INT >= 26) {
              const intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS')
              intent.putExtra('android.provider.extra.APP_PACKAGE', pkName)
              main.startActivity(intent) // 跳转到该应用的系统通知设置页
            } else if (Build.VERSION.SDK_INT >= 21) {
              // android 5.0-7.0
              const intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS')
              intent.putExtra('app_package', pkName)
              intent.putExtra('app_uid', uid)
              main.startActivity(intent) // 跳转到该应用的系统通知设置页
            }
          }
        },
      })
    }
  } else if (plus.os.name === 'iOS') {
    // 判断是ISO
    let isOn = false
    let types = 0
    const app = plus.ios.invoke('UIApplication', 'sharedApplication')
    const settings = plus.ios.invoke(app, 'currentUserNotificationSettings')
    if (settings) {
      types = settings.plusGetAttribute('types')
      plus.ios.deleteObject(settings)
    } else {
      types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes')
    }
    plus.ios.deleteObject(app)
    isOn = types !== 0
    if (isOn === false) {
      uni.showModal({
        title: '通知权限开启提醒',
        content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
        showCancel: true,
        confirmText: '去设置',
        cancelText: '稍后设置',
        success: function (res) {
          if (res.confirm) {
            const app = plus.ios.invoke('UIApplication', 'sharedApplication')
            const setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:')
            plus.ios.invoke(app, 'openURL:', setting)
            plus.ios.deleteObject(setting)
            plus.ios.deleteObject(app)
          }
        },
      })
    }
  }
  // #endif
}
